<?php

// @formatter:off
/**
 * @file tagsof.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: tagsof.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/info/tagsof.php $
 */
// @formatter:on

class ActionControllerInfoTagsof extends WCActionController {
	//
	// Magic methods.
	public function __construct() {
		parent::__construct();

		if(isset($_REQUEST["removable"])) {
			$this->_selector["extra"] = "R";
		}
		if(isset($_REQUEST["editable"])) {
			$this->_selector["extra"] = "E";
		}
	}
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcComic;
			global $wcImage;

			if(!$wcProfile->allowedTo(WC_PERM_TAG_LIST, WC_PERM_TAG_LIST)) {
				$check = WC_HTTPERROR_FORBIDDEN;

				global $errno;
				global $error;

				$errno = $check;
				$error = "You're not allowed to list tags";
			}
			if($check == WC_HTTPERROR_OK && (!$wcComic || !$wcComic->ok())) {
				if(!$wcImage || !$wcImage->ok()) {
					$check = WC_HTTPERROR_BAD_REQUEST;

					global $errno;
					global $error;

					$errno = $check;
					$error = "You haven't specified neight a comic nor an image";
				}
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;
		//
		// Loading global pointers.
		global $wcComic;
		global $wcImage;

		$tags = array();
		$list = array();

		if($wcComic && $wcComic->ok()) {
			$list = WCTagsHolder::I()->tagsOfComic($wcComic);
			$this->_manager->edit_what = "Comic";
			$this->_manager->edit_id = $wcComic->id();
			$this->_manager->edit_name = $wcComic->name();
			$this->_manager->edit_uri_name = "comic";
			$this->_manager->edit_type = "CX";
		} elseif($wcImage && $wcImage->ok()) {
			$list = WCTagsHolder::I()->tagsOfImage($wcImage);
			$this->_manager->edit_what = "Image";
			$this->_manager->edit_id = $wcImage->id();
			$this->_manager->edit_name = $wcImage->name();
			$this->_manager->edit_uri_name = "image";
			$this->_manager->edit_type = "IM";
		}

		foreach($list as $tag) {
			$aux = array(
				"name" => $tag->name(),
				"id" => $tag->id()
			);

			if(WCActiveTagsHolder::I()->isActive($tag->name())) {
				$aux["special"] = true;
			}

			$pieces = explode(":", $tag->name());
			if(count($pieces) > 1) {
				$aux["type"] = $pieces[0];
			}

			$tags[$tag->name()] = $aux;
		}

		ksort($tags);
		$this->_manager->tags = array_values($tags);

		if(isset($_REQUEST["boxid"])) {
			$this->_manager->boxid = $_REQUEST["boxid"];
		}
		if(isset($_REQUEST["editable"])) {
			$this->_manager->editable = true;
		}
		if(isset($_REQUEST["removable"])) {
			$this->_manager->removable = true;
		}

		return $ok;
	}
}
